<html>
<head>
<script type="text/javascript">
var player;
var game;
var settings_object;

function page_loaded() {
	chrome.extension.sendRequest({method: "read_settings"}, function(response) {
		player = document.location.href.split('?')[1].split('&')[0].split('=')[1];
		game = unescape(document.location.href.split('?')[1].split('&')[1].split('=')[1]);
		settings_object = response.settings_object;
		settings_loaded();
	});
}

function settings_loaded() {
	var status_span = document.getElementById("status_span");
	status_span.innerHTML="Edit Player: "+player+" in \""+game+"\"";
	load_player();
}

function  load_player() {
	var edit_player_character_tag = document.getElementById('edit_player_character_tag');
	var edit_player_character_image_url = document.getElementById('edit_player_character_image_url');
	var edit_player_character_detail = document.getElementById('edit_player_character_detail');
	var edit_player_sheet_thread = document.getElementById('edit_player_sheet_thread');
	var edit_player_image = document.getElementById('edit_player_image');
	
	edit_player_character_tag.value = settings_object.games[game].players[player].character_tag;
	edit_player_character_image_url.value = settings_object.games[game].players[player].character_image_url;
	edit_player_character_detail.value = settings_object.games[game].players[player].character_detail;
	edit_player_sheet_thread.value = settings_object.games[game].players[player].sheet_thread;
	redraw_image ();
}

function redraw_image () {
	var edit_player_image = document.getElementById('edit_player_image');
	var edit_player_character_image_url = document.getElementById('edit_player_character_image_url');
	if (edit_player_character_image_url.value) {
		edit_player_image.src = edit_player_character_image_url.value;
	} else {
		edit_player_image.src = "none";
	};
	
}

function update_player() {
	var edit_player_userid = document.getElementById('edit_player_userid');
	var edit_player_gametag = document.getElementById('edit_player_gametag');
	var edit_player_character_tag = document.getElementById('edit_player_character_tag');
	var edit_player_character_image_url = document.getElementById('edit_player_character_image_url');
	var edit_player_character_detail = document.getElementById('edit_player_character_detail');
	var edit_player_sheet_thread = document.getElementById('edit_player_sheet_thread');
	
	settings_object.games[game].players[player].character_tag = edit_player_character_tag.value;
	settings_object.games[game].players[player].character_image_url = edit_player_character_image_url.value;
	settings_object.games[game].players[player].character_detail = edit_player_character_detail.value;
	settings_object.games[game].players[player].sheet_thread = edit_player_sheet_thread.value;

		chrome.extension.sendRequest({method: "write_settings_object", settings_object: settings_object}, function(response) {
			
			if (response) {
				//console.log('wrote settings object');
				var status_result = document.getElementById("status_result");
				status_result.innerHTML = "Player Saved.";
				dirtyPage("player "+player+" updated in "+game);
				setTimeout(function() {
					status_result.innerHTML = "";
					//restore_options();
				}, 750);
				
			} else {
				//console.log('write returned false');
			};
		});
}

function dirtyPage(message) {
	chrome.tabs.getSelected(null, function (tab) {
		chrome.tabs.sendRequest(tab.id, {method: "make_page_dirty", message: message}, function(response) {
			console.log(response.answer);
		});
	});
}
</script>
</head>
<body onload="page_loaded()" style="padding:16px;font-family: verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;font-size: 16px; background-color:#3C3027;">
<div style="min-width:360px; min-height:200px; border: 1px solid black; background-color: white;">
<span id="status_span"></span><br />
<div id="edit_player">
<label for="edit_player_character_tag">character_tag:</label><input id="edit_player_character_tag"></input><br />
<label for="edit_player_character_image_url" title="click > button to preview">character_image_url:</label><input id="edit_player_character_image_url"></input><button onclick="redraw_image ()">\/</button><br />
<img style="float:right; width: 60px; height:60px;" id="edit_player_image" />
<label for="edit_player_character_detail">character_detail:</label><input id="edit_player_character_detail"></input><br />
<label for="edit_player_sheet_thread">sheet_thread:</label><input id="edit_player_sheet_thread"></input><br />


<button onclick="update_player()">Update</button><button onclick="window.close()">Cancel/Close</button><br />
<span id="status_result"></span><br />
</div>
</div>
</body>
</html>